package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.GoodsStyle;
import com.gxa.zhonglong.service.GoodsStyleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/11/01 13:17
 * Description:
 * Version: V1.0
 */
@Api(tags = "商品款式类型api")
@CrossOrigin
@RestController
@RequestMapping("/goodsStyle")
public class GoodsStyleController {
    @Autowired
    private GoodsStyleService goodsStyleService;

    @PostMapping("/selectAll")
    @ApiOperation("查询所有商品款式类型")
    public Map<String,Object> selectAll(){
        List<Map<String,Object>> list = goodsStyleService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改商品款式类型")
    public Map<String,Object> addOrUpdate(GoodsStyle goodsStyle) throws BusinessException {
        JsonResult jsonResult = new JsonResult();
        if (goodsStyle.getId()==null) {
            goodsStyleService.add(goodsStyle);
            jsonResult.setMsg("添加成功!");
        }else {
            goodsStyleService.update(goodsStyle);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/del")
    @ApiOperation("删除/多删除商品款式类型(单删除也传存id的数组)")
    public Map<String,Object> del(@RequestBody Map<String, List<Long>> ids) throws BusinessException {
        goodsStyleService.del(ids.get("ids"));
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }
}
